package com.huihui.fragment.main;


import android.content.res.Resources;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.huihui.R;
import com.huihui.adapter.FragmentAdapter;
import com.huihui.fragment.BaseFragment;
import com.huihui.fragment.ChaozhiFragment;
import com.huihui.fragment.DiscussFragment;
import com.huihui.fragment.NewsFragment;
import com.huihui.fragment.SmallVideoFragment;
import com.huihui.utils.IntentUtils;
import com.huihui.utils.ToolUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;

/**
 * Created by Administrator on 2018/8/13.
 */

public class HomeFragment extends BaseFragment {
    private ImageView content_type_select;
    private LinearLayout search_tip;
    private TabLayout tablayout;
    private ViewPager viewPager;

    private ArrayList<Fragment> fragmentsList = new ArrayList<>();
    //添加tablayout的内容
    private ArrayList<String> title = new ArrayList<>();
    private NewsFragment newsFragment;
    private ChaozhiFragment chaozhiFragment;
    private SmallVideoFragment smallVideoFragment;
    private DiscussFragment discussFragment;
    private LinearLayout classLayout;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home,container,false);
        initView(view);
        initListener();
        return view;
    }


    private void initListener() {
        search_tip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //搜索
                IntentUtils.gotoSearchActivity(getContext());
            }
        });
//        content_type_select.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//            }
//        });
        classLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    private void initView(View view) {
        content_type_select = view.findViewById(R.id.content_type_select);
        search_tip = view.findViewById(R.id.search_tip);
        tablayout = view.findViewById(R.id.tablayout);
        viewPager = view.findViewById(R.id.viewpager);
        classLayout = view.findViewById(R.id.articile_ll);
        initTabs();
    }

    private void initTabs() {
        title.add("最新");
        title.add("超值");
//        title.add("视频");
        title.add("讨论");

        newsFragment = new NewsFragment();
        chaozhiFragment = new ChaozhiFragment();
        smallVideoFragment =new SmallVideoFragment();
        discussFragment =new DiscussFragment();

        fragmentsList.add(newsFragment);
        fragmentsList.add(chaozhiFragment);
//        fragmentsList.add(smallVideoFragment);
        fragmentsList.add(discussFragment);


        FragmentAdapter myPagerAdapter = new FragmentAdapter(getFragmentManager(), fragmentsList, title);
        tablayout.setTabsFromPagerAdapter(myPagerAdapter);
        viewPager.setAdapter(myPagerAdapter);
        tablayout.setupWithViewPager(viewPager);
//        setIndicator(tablayout, ToolUtils.px2dp(getContext(),80), ToolUtils.px2dp(getContext(),80));
        int width = ToolUtils.getWidth(getContext());
        Paint mPaint = new Paint();
        Rect rect = new Rect();
        mPaint.setStrokeWidth(ToolUtils.px2dp(getContext(),34));
        mPaint.getTextBounds("超值最新超值最新",0,8,rect);
        //计算单个tap的最小宽度
        int itemWidth = rect.width();
        //计算单个tap左右间距
//        int margin = (width/4 - itemWidth)/2;
        int margin = (width/3 - itemWidth)/2;
        setTabLine(tablayout,itemWidth,margin);
    }

    public void setTabLine(TabLayout tab,int itemWidth,int margin){
        try {
            Class<?> tablayout = tab.getClass();
            Field tabStrip = tablayout.getDeclaredField("mTabStrip");
            tabStrip.setAccessible(true);
            LinearLayout ll_tab= (LinearLayout) tabStrip.get(tab);
            for (int i = 0; i < ll_tab.getChildCount(); i++) {
                View child = ll_tab.getChildAt(i);
                child.setPadding(0,0,0,0);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT,1);
                //修改两个tab的间距
                params.leftMargin = margin;
                params.rightMargin = margin;
                //设置tap的宽度
                params.width = itemWidth;
                child.setLayoutParams(params);
                child.invalidate();
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
